package com.sherlockHolmes.init.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sherlockHolmes.init.entity.dao.Application;
import com.sherlockHolmes.init.entity.dao.ApplyCondition;
import com.sherlockHolmes.init.entity.dao.UserInfo;
import com.sherlockHolmes.init.entity.dto.AssetDTO;
import com.sherlockHolmes.init.service.ApplicationService;
import com.sherlockHolmes.init.mapper.ApplicationMapper;
import com.sherlockHolmes.init.service.UserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
* @author 四月一日 君寻
* &#064;description  针对表【application(申请表)】的数据库操作Service实现
* &#064;createDate  2024-03-27 11:48:55
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application>
    implements ApplicationService{
    @Resource
    private ApplicationMapper applicationMapper;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 申请记录
     */
    @Override
    public List<Application> unApplication(Integer communityId,int applicationResult) {
        return new LambdaQueryChainWrapper<>(applicationMapper)
                .eq(Application::getCommunityId,communityId)
                .eq(Application::getApplicationResult,applicationResult)
                .eq(Application::getValid,0)
                .list();
    }

    /**
     * 退返申请费用
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void refunds(UserInfo userInfo, List<Integer> userIds, ApplyCondition applyCondition) {
        if(ObjectUtil.isNotEmpty(applyCondition)){
            Double total=0.00;
            if("2".equals(applyCondition.getType())){//退硬币
                //统计总共需要退多少硬币
                for (Integer userId:userIds
                     ) {
                    Integer num = applyCondition.getNum();
                    total+=num;
                }
                //判断创建人的资产是否足够
                if(userInfo.getCoin().compareTo(BigDecimal.valueOf(total))>=0){
                    userInfoService.reduceAsset(new AssetDTO("2",total,userInfo.getId()));//扣除创建人资产
                    for (Integer userId:userIds
                    ) {
                      userInfoService.addAsset(new AssetDTO("2",applyCondition.getNum(),userId));//退回成员费用
                    }
                }else {
                    throw new RuntimeException("硬币不足,请先充值");
                }
            } else if ("3".equals(applyCondition.getType())) {//退金币
                if(userInfo.getGold().compareTo(BigDecimal.valueOf(total))>=0){
                    userInfoService.reduceAsset(new AssetDTO("1",total,userInfo.getId()));//扣除创建人资产
                    for (Integer userId:userIds
                    ) {
                        userInfoService.addAsset(new AssetDTO("1",applyCondition.getNum(),userId));//退回成员费用
                    }
                }else {
                    throw new RuntimeException("金币不足,请先充值");
                }
            }
        }
    }
}




